package com.jacklin.mapper;

import com.jacklin.entity.ProductCategory;
import org.apache.ibatis.annotations.*;

import java.util.Map;

/**
 * @Author 林必昭
 * @Date 2019/4/25 9:41
 */
public interface ProductCategoryMapper {

    //第一种方式
    @Insert("insert into product_category(category_name,category_type) value(#{category_name,jdbcType=VARCHAR},#{category_type,jdbcType=INTEGER})")
    int insertByMap(Map<String,Object> map);

    //第二种方式
    @Insert("insert into product_category(category_name,category_type) value(#{categoryName,jdbcType=VARCHAR},#{categoryType,jdbcType=INTEGER})")
    int insertByObject(ProductCategory productCategory);

    /**
     * 通过category_type查询
     * @param categoryType
     * @return
     */
    @Select("select * from product_category where category_type =#{categoryType}")
    @Results({
            @Result(column = "category_id",property = "categoryId" ),
            @Result(column = "category_name",property = "categoryName" ),
            @Result(column = "category_type",property = "categoryType" )
    })
    ProductCategory findByCategoryType(Integer categoryType);

    /**
     * 通过category_name查询
     * @param categoryName
     * @return
     */
    @Select("select * from product_category where category_name =#{categoryName}")
    @Results({
            @Result(column = "category_id",property = "categoryId" ),
            @Result(column = "category_name",property = "categoryName" ),
            @Result(column = "category_type",property = "categoryType" )
    })
    ProductCategory findByCategoryName(String categoryName);

    /**
     * 如果是多个字段传入的话，要使用@Param指定
     * @param categoryName
     * @param categoryType
     * @return
     */
    @Update("update product_category set category_name=#{categoryName} where category_type = #{categoryType}")
    int updateByCategoryType(@Param("categoryName") String categoryName,
                             @Param("categoryType") Integer categoryType);

    /**
     * 根基对象更新
     * @param productCategory
     * @return
     */
    @Update("update product_category set category_name=#{categoryName} where category_type = #{categoryType}")
    int updateByObject(ProductCategory productCategory);


    @Delete("delete from product_category where category_type = #{categoryType}")
    int deleteByCategoryType(Integer categoryType);


    /**
     * 使用xml配置形式，注意新建的xxxMapper.xml要与类对应
     * @param categoryType
     * @return
     */
    ProductCategory selectByCategoryType(Integer categoryType);
}
